<template>
  <div>
    <a-drawer
      width="1100"
      :maskClosable="false"
      :visible="dialogData.show"
      direction="rtl"
      @close="handleClose"
    >
      <div slot="title">
        <!-- <i class="el-icon-arrow-left"></i> -->
        <span>{{dialogData.title}}</span>
      </div>
      
      <el-row style="width:1120px;padding-bottom:150px;height:100%;overflow-y:auto" v-loading="loading">
        <el-col :span="18">
          <el-form
            :model="parames"
            :rules="rules"
            label-position="left"
            ref="paramesb"
            label-width="94px"
            class="addForm"
            size="small"
          >
            <el-row style="padding: 0 40px">
              <el-col :span="24">
                <a-collapse :defaultActiveKey="activeNames" :bordered="false">
                  <a-collapse-panel class="marginB10" header="基础信息" key="1">
                    <el-row :gutter="40" style="margin-top: 14px">
                      <el-col :span="12">
                        <el-form-item label="客户标签:" >
                          <a-select mode="tags" style="width: 100%" v-model="parames.customerLabel" :tokenSeparators="[',']">
                            <a-select-option v-for="item in customerLabelData" :key="item.description">{{item.description}}</a-select-option>
                          </a-select>
                        </el-form-item>
                        <!-- <el-form-item label="客户ID:" >
                          <el-input disaled v-model="parames.phone"></el-input>
                        </el-form-item> -->
                      </el-col>
                      <el-col :span="24">
                        <el-row :gutter="40">
                          <el-col :span="12">
                            <el-form-item label="公司名称:" prop="customerName">
                              <el-input size="small" v-model="parames.customerName" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="12">
                            <el-form-item label="简称:">
                              <el-input size="small" v-model="parames.shortName" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="12">
                            <el-form-item label="曾用名:" >
                              <el-input v-model="parames.previousName" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                        </el-row>
                      </el-col>
                      
                      <el-col :span="24">
                        <el-row :gutter="40">
                          <el-col :span="12">
                            <el-form-item label="社会信用代码:">
                              <el-input size="small" v-model="parames.creditCode" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="12">
                            <el-form-item label="客户等级:">
                              <el-select disabled style="width:100%" size="small" v-model="parames.customerLevel">
                                <el-option v-for="(item,index) in customerLevel" :key="index" :label="item.desc" :value="item.name"></el-option>
                              </el-select>
                            </el-form-item>
                          </el-col>
                          <el-col :span="12">
                            <el-form-item label="企业电话:">
                              <el-input size="small" v-model="parames.businessPhone" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="12">
                            <el-form-item label="企业邮箱:">
                              <el-input size="small" v-model="parames.businessEmail" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="24">
                            <el-form-item label="联系地址:">
                              <el-input size="small" v-model="parames.address" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                          <el-col :span="24">
                            <el-form-item label="备注:">
                              <el-input size="small" v-model="parames.remark" placeholder="请输入"></el-input>
                            </el-form-item>
                          </el-col>
                        </el-row>
                      </el-col>
                      <el-col :span="24">
                        <el-row>
                          <el-col :span="12">
                            <el-form-item label="客户来源:" prop="customerSourceId" >
                              <shangJi v-model="parames.customerSourceId" @checkValue="checkValue" :typeid="parames.customerSourceType" :id="parames.customerSourceId" :type="dialogData.type == 'new'?'new':'edit'"></shangJi>
                            </el-form-item>
                          </el-col>
                        </el-row>
                      </el-col>
                      <!-- <el-col :span="24">
                        <el-row>
                          <el-col :span="12">
                            <el-form-item label="负责人:" prop="director">
                              <userTree v-model="parames.director" :currentValue="parames.director"></userTree>
                            </el-form-item>
                          </el-col>
                        </el-row>
                      </el-col> -->
                    </el-row>
                  </a-collapse-panel>
                  <a-collapse-panel v-if="dialogData.type == 'edit'" class="marginB10" header="工商信息" key="5">
                    <el-row :gutter="40">
                      <el-col :span="12" style="margin-top:15px">
                        <el-form-item label="主营行业:">
                          <el-cascader
                            style="width:265px"
                            v-model="gsData.industry"
                            :options="subjectData">
                          </el-cascader>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12" style="margin-top:15px">
                        <el-form-item label="企业类型:">
                          <el-select style="width:100%" size="small" v-model="gsData.companyTypeId">
                            <el-option v-for="(item,index) in companyData" :key="index" :label="item.typeName" :value="item.id"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="法定代表人:">
                          <el-input v-model="gsData.artificialPerson" placeholder="请输入"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="注册资金:">
                          <el-input placeholder="请输入" type="number" v-model="gsData.registeredCapital">
                            <template slot="append">万元</template>
                          </el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="成立日期:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.establishmentDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="出执照日期:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.licenseDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="工商开始时间:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.businessStartDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="工商结束时间:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.businessEndDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="营业期限截止:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.businessDeadlineDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="核准日期:">
                          <el-date-picker
                          style="width:100%"
                          v-model="gsData.approvalDate"
                          type="date"
                          placeholder="选择日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="注册区域:">
                          <el-cascader
                          placeholder="请选择"
                          style="width:100%"
                          :options="options"
                          v-model="gsData.administrativeRegion"
                          >
                          </el-cascader>
                        </el-form-item>
                      </el-col>
                      <!-- <el-col :span="12">
                        <el-form-item label="注册区域:">
                          <el-input v-model="gsData.administrativeRegion" placeholder="请输入"></el-input>
                        </el-form-item>
                      </el-col> -->
                      <el-col :span="24">
                        <el-form-item label="注册详细地址:">
                          <el-input v-model="gsData.registeredAddress" placeholder="请输入"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="24">
                        <el-form-item label="经营范围:">
                          <el-input v-model="gsData.businessScope" placeholder="请输入"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </a-collapse-panel>
                  <a-collapse-panel class="marginB10" header="人员信息" key="2">
                    <el-table
                      v-loading="contacloading"
                      :data="parames.customerContactsList"
                      style="width: 100%">
                      <el-table-column
                        prop="contactsName"
                        label="姓名"
                        width="180">
                      </el-table-column>
                      <el-table-column
                        prop="roleNames"
                        label="角色">
                        <template slot-scope='scope'>
                          <span v-if="scope.row.contactsRoleName">{{scope.row.contactsRoleName.join(',')}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column
                        prop="contactsMobile"
                        label="手机号">
                      </el-table-column>
                      <el-table-column
                        prop="remark"
                        label="备注">
                      </el-table-column>
                      <el-table-column
                        prop="address"
                        width="70"
                        label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEditUser(scope.$index,scope.row)"></el-button>
                            <a-popconfirm
                              title="是否确认删除?"
                              @confirm="handleCanleUser(scope.$index,scope.row)"
                              okText="是"
                              cancelText="否"
                            >
                              <el-button size="mini" type="text" icon="el-icon-delete"></el-button>
                            </a-popconfirm>
                        </template>
                      </el-table-column>
                    </el-table>
                    <el-button icon="el-icon-plus" size="small" style="width:100%;margin-top:20px" @click="handleAddUser">添加人员</el-button>
                  </a-collapse-panel>
                  <a-collapse-panel v-if="dialogData.type == 'edit'" class="marginB10" header="财税信息" key="6">
                    <el-row style="margin-top:15px" :gutter="40">
                      <el-col :span="12">
                        <el-form-item label="财税类型:">
                          <el-select style="width:100%" size="small" v-model="cwData.taxType">
                            <el-option v-for="(item,index) in taxType" :key="index" :label="item.desc" :value="item.name"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="报税类别:">
                          <el-select style="width:100%" size="small" v-model="cwData.declareTaxType">
                            <el-option v-for="(item,index) in declarTaxType" :key="index" :label="item.desc" :value="item.name"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="是否零申报:">
                          <el-select style="width:100%" size="small" v-model="cwData.zeroDeclaration">
                            <el-option label="是" :value="true"></el-option>
                            <el-option label="否" :value="false"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="税务所:">
                          <el-input placeholder="请输入" v-model="cwData.taxOffice"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="税盘:">
                          <el-select style="width:100%" size="small" v-model="cwData.taxPlate">
                            <el-option v-for="(item,index) in taxPlate" :key="index" :label="item.desc" :value="item.name"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="专营员姓名:">
                          <el-input placeholder="请输入" v-model="cwData.franchiseeName"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="专营员电话:">
                          <el-input placeholder="请输入" v-model="cwData.franchiseePhone"></el-input>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </a-collapse-panel>
                  <a-collapse-panel class="marginB10" header="文档资料" key="3">
                    <el-table
                      :data="parames.customerFileList"
                      style="width: 100%">
                      <el-table-column
                        prop="fileName"
                        label="文件名称"
                        width="180">
                      </el-table-column>
                      <el-table-column
                        prop="fileType"
                        label="文件类型">
                      </el-table-column>
                      <el-table-column
                        prop="fileSize"
                        label="大小">
                        <template slot-scope="scope">
                            <p>{{scope.row.fileSize}}kb</p>
                        </template>
                      </el-table-column>
                      <!-- <el-table-column
                        prop="address"
                        label="最近上传时间">
                      </el-table-column> -->
                      <!-- <el-table-column
                        prop="address"
                        label="上传人">
                      </el-table-column> -->
                      <el-table-column
                        prop="address"
                        width="70"
                        label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEditFile(scope.$index,scope.row)"></el-button>
                            <a-popconfirm
                              title="是否确认删除?"
                              @confirm="handleCanleFile(scope.$index,scope.row)"
                              okText="是"
                              cancelText="否"
                            >
                              <el-button size="mini" type="text" icon="el-icon-delete"></el-button>
                            </a-popconfirm>
                            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleCanleFile(scope.$index,scope.row)"></el-button> -->
                        </template>
                      </el-table-column>
                    </el-table>
                    <el-button icon="el-icon-upload2" size="small" @click="handleFile" style="width:100%;margin-top:20px">上传文件</el-button>
                  </a-collapse-panel>
                  <a-collapse-panel class="marginB10" header="账户信息" key="4">
                    <el-row style="margin-top: 20px" :gutter="14">
                      <el-col :span="8" v-for="(item,index) in parames.customerAccountList" :key="index">
                        <el-card class="box-card" shadow="never">
                          <div slot="header" class="clearfix">
                            <span v-if="item.accountType == 'BANK'">银行账户</span>
                              <span v-if="item.accountType == 'ALIPAY'">支付宝账户</span>
                              <span v-if="item.accountType == 'WE_CHAT'">微信账户</span>
                              <span v-if="item.accountType == 'CASH'">现金账户</span>
                            <!-- <el-button style="float: right; padding: 3px 0;" icon="el-icon-delete" type="text" @click="handleCanleAccount(index,item)"></el-button> -->
                            <a-popconfirm
                              title="是否确认删除?"
                              @confirm="handleCanleAccount(index,item)"
                              okText="是"
                              cancelText="否"
                            >
                              <el-button style="float: right; padding: 3px 0;" icon="el-icon-delete" type="text"></el-button>
                            </a-popconfirm>
                            <el-button style="float: right; padding: 3px 0;margin-right: 10px" icon="el-icon-edit" type="text" @click="handleEditAccount(index,item)"></el-button>
                          </div>
                          <div>
                            <p>{{item.cardNumber}}</p>
                            <p>{{item.realName}}</p>
                          </div>
                        </el-card>
                      </el-col>
                    </el-row>
          
                    <el-button icon="el-icon-plus" size="small" style="width:100%;margin-top:20px" @click="handleAddAccount">添加账户</el-button>
                  </a-collapse-panel>
                </a-collapse>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="5" style="position: fixed;top:80px;right:-10px;width:240px">
          <div v-if="dialogData.type == 'edit'">
            <el-button
              @click="handleCancle"
              size="small"
              icon="el-icon-close"
              style="width:200px;text-align: left;margin-bottom:10px"
            >取消编辑</el-button>
          </div>
          <div class="form-team-box">
            <p class="formtitle" v-if="parames.userIdList">团队成员（{{parames.userIdList.length}}）人</p>
            <el-row style="margin-top:10px">
              <span v-for="(item,index) in parames.userIdList" :key="index">
                  <el-col style="margin-bottom:10px" v-if="index==0" >
                    <span class="team-icon" style="background: #409efe" v-if="item.userName">{{item.userName.slice(0,1)}}</span>
                    <span class="team-name">{{item.userName}}</span>
                  </el-col>
                  <el-col style="margin-bottom:10px" v-if="index>0" >
                    <span class="team-icon" style="background: #409efe" v-if="item.userName">{{item.userName.slice(0,1)}}</span>
                    <span class="team-name">{{item.userName}}</span>
                    <i class="el-icon-delete team-del" @click="handleDeleteUser(index,item)"></i>
                  </el-col>
                </span>
            </el-row>
            <el-button @click="handleNewTeam" type="text" style="margin-top:14px" icon="el-icon-plus">添加新成员</el-button>

          </div>
        </el-col>
      </el-row>

      <div class="from-bottom-box">
        <el-button size="small" type="primary" :loading="loadingBtn" @click="handleSend">保存</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </div>
    </a-drawer>
    <addUserhDialog :addUserData="addUserData" @addCustomer="addCustomer" @checkRender="checkRenderUser"></addUserhDialog>
    <addFileDialog :addFileData="addFileData" @addFile="addFile" @checkRender="checkRenderFile"></addFileDialog>
    <addAccountDialog :addAccountData="addAccountData" @addAccount="addAccount" @checkRender="checkRenderAccount"></addAccountDialog>
    <teamDialog :teamData="teamData" @selectUser="selectUser"></teamDialog>

  </div>
</template>

<script>
import addUserhDialog from "./addCustomerUser.vue"; //添加人员详情
import addFileDialog from "./uploadDialog.vue"; //添加文件
import addAccountDialog from "./accountDialog.vue" //添加账户
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'

export default {
  props: ["dialogData"],
  data() {
    
    return {
      loading: false,
      loadingBtn: false,
      options: regionData,
      contacloading: false,
      activeNames: ['1','2','3','4', '5', '6'],
      customerLabelData: [], //客户标签
      customerLevel: [], //客户等级
      taxType: [], //财税类型
      declarTaxType: [], //报税类别
      taxPlate: [], //税盘枚举
      addUserData: {
        customerId: '', //客户id
        title: '',
        id: '', //客户人员id
        type: '',
        data: [],
        show: false
      },
      addFileData: { //附件弹窗
        customerId: '', //客户id
        title: '',
        id: '', //客户人员id
        type: '',
        data: {},
        show: false
      },
      addAccountData: { //账户弹窗
        customerId: '', //客户id
        title: '',
        id: '', //客户人员id
        type: '',
        data: {},
        show: false
      },
      parames: {
        id: '',
        customerLabel: [], //客户标签
        customerName: "", //客户名称
        shortName: "", //简称
        previousName: "", //曾用名
        creditCode: '', //社会信用代码
        customerLevel: 'POTENTIAL_CUSTOMER', //客户等级
        businessPhone: '', //企业电话
        businessEmail: '', //企业邮箱
        address: '', //联系地址
        remark: '', //备注
        customerSourceType: '', //客户来源类型
        customerSourceId: '', //客户来源
        // director: '', //负责人
        customerContactsList: [], //人员信息
        customerFileList: [], //文件列表
        customerAccountList: [], //账户信息
        userIdList: [], //团队成员
      },
      teamData: {
        show: false
      },
      gsData: {
        customerId: '',//客户ID,
        industry: [],
        childIndustryId: '',//细分行业id
        industryId: '', //主营行业ID,
        companyTypeId: '', //企业类型ID,
        artificialPerson: '', //法定代表人,
        registeredCapital: '', //注册资金,
        establishmentDate: '', //成立日期,
        licenseDate: '',//出执照日期,
        businessDeadlineDate: '', //营业期限截止,
        approvalDate: '',//核准日期,
        administrativeRegion: [], //注册行政区域ID,
        registeredRegionId: [],
        registeredAddress: '', //注册详细地址,
        businessScope: '', //经营范围ID
        businessEndDate: '',
        businessStartDate: ''
      },
      cwData: {
        customerId: '', //客户ID,
        taxType: '', //财税类型（枚举）,
        declareTaxType: '', //报税类别（枚举）,
        zeroDeclaration: '', //是否零申报（布尔）,
        taxOffice: '', //税务所,
        taxPlate: '', //税盘（枚举）,
        franchiseeName: '', //专营员姓名,
        franchiseePhone: '', //专营员电话
      },
      companyData: [], //企业类型
      subjectData: [], //主营行业
      rules: {
        customerName: [
          { required: true, message: "请输入客户名称", trigger: ['blur','change']},
        ],
        customerSourceId: [
          { required: true, message: "请选择客户来源", trigger: ['blur']},
        ],
        // director: [
        //   { required: true, message: "请选择负责人", trigger: ['blur','change']},
        // ],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    addUser () {
      this.searchData.show = true
    },
    checkRender () {
      this.render()
    },
    handleCancle () { //取消重新编辑
      this.dialogData.show = false
      this.$emit('checkOpen')
    },
    handleNewTeam () { //添加新成员
        this.teamData.show = true
      },
      selectUser (value) { //添加选择的成员
        if (this.dialogData.type == 'new') {
          let select = value //选中的数组
          let origan = this.parames.userIdList //本身存在的数组
          select.forEach(el=>{
            let i = 0
            origan.forEach((bl,index)=>{
              if (el.userId == bl.userId) {
                i = 1
              }
            })
            if (i !== 1) {
              this.parames.userIdList.push(el)
            }
          })
        } else {
          let select = value //选中的数组
          let userIdList = []
          let origan = this.parames.userIdList //本身存在的数组
          select.forEach(el=>{
            let i = 0
            origan.forEach((bl,index)=>{
              if (el.userId == bl.userId) {
                i = 1
              }
            })
            if (i !== 1) {
              userIdList.push(el.userId)
            }
          })
          this.$post(this.$api.postOpportunityTeamMemberAdd,{
            teamId: this.dialogData.id,
            userIdList: userIdList
          }).then(response => {
            if (response.data.succeed) {
              this.render()
            }
          })
        }
      },
      handleDeleteUser (index,item) { //删除组内人员
        if (item.teamMemberId) {
          this.$post(this.$api.postOpportunityTeamMemberDel,{
            teamMemberId: item.teamMemberId
          }).then(response => {
            if (response.data.succeed) {
              this.$message({ type:'success',message: '删除成功'})
              this.parames.userIdList.splice(index,1)
            }
          })
        } else {
          this.parames.userIdList.splice(index,1)
        }
      },
    handleAddUser () { //添加客户人员
      this.addUserData.title = '新增成员信息'
      this.addUserData.index = ''
      if (this.dialogData.type == 'new') {
        this.addUserData.type = 'new'
      } else if (this.dialogData.type == 'edit') {
        this.addUserData.type = 'saveNew'
        this.addUserData.data = []
        this.addUserData.id = this.dialogData.id
      }
      this.addUserData.data = []
      this.addUserData.show = true
    },
    addCustomer (value,type,index) { //新建时-修改人员信息的回调
      if (type == 'new') {
        this.parames.customerContactsList.push(JSON.parse(value))
      } else if (type == 'newEdit') { //新建时-修改人员信息
        this.parames.customerContactsList.splice(index,1,JSON.parse(value))
      }
    },
    handleEditUser (index,row) { //修改添加的人员信息
      this.addUserData.title = '修改成员信息'
      this.addUserData.index = index
      if (this.dialogData.type == 'new') {
        this.addUserData.type = 'newEdit'
      } else if (this.dialogData.type == 'edit') {
        this.addUserData.type = 'saveEdit'
      }
      this.addUserData.data = JSON.stringify(row)
      this.addUserData.show = true
    },
    handleCanleUser (index,row) { //删除添加的人员信息
      if (this.dialogData.type == 'new') {
        this.parames.customerContactsList.splice(index,1)
      } else if (this.dialogData.type == 'edit') {
        this.$post(this.$api.postCustomerContactsDel,{
          customerContactsId: row.id
        }).then(response => {
          if (response.data.succeed) {
            this.render()
          }
        })
      }
    },
    handleFile() { //上传附件
      this.addFileData.title = '上传附件'
      if (this.dialogData.type == 'new') {
        this.addFileData.type = 'new'
      } else if (this.dialogData.type == 'edit') {
        this.addFileData.type = 'saveNew'
        this.addFileData.id = this.dialogData.id
      }
      this.addFileData.show = true
    },
    addFile (value,type,index) { //新建时-上传附件回调
      if (type == 'new') {
        this.parames.customerFileList.push(JSON.parse(value))
      } else if (type == 'newEdit') { //新建时-修改人员信息
        this.parames.customerFileList.splice(index,1,JSON.parse(value))
      }
    },
    handleEditFile (index,row) { //修改添加的附件
      this.addFileData.title = '修改文件信息'
      this.addFileData.index = index
      if (this.dialogData.type == 'new') {
        this.addFileData.type = 'newEdit'
      } else if (this.dialogData.type == 'edit') {
        this.addFileData.type = 'saveEdit'
      }
      this.addFileData.data = JSON.stringify(row)
      this.addFileData.show = true
    },
    handleCanleFile (index,row) { //删除添加的附件
      if (this.dialogData.type == 'new') {
        this.parames.customerFileList.splice(index,1)
      } else if (this.dialogData.type == 'edit') {
        this.$post(this.$api.postCustomerFileDel,{
          customerFileId: row.id
        }).then(response => {
          if (response.data.succeed) {
            this.render()
          }
        })
      }
    },
    handleAddAccount () { //添加账户信息
      this.addAccountData.title = '新增账户'
      this.addAccountData.index = ''
      if (this.dialogData.type == 'new') {
        this.addAccountData.type = 'new'
      } else if (this.dialogData.type == 'edit') {
        this.addAccountData.type = 'saveNew'
        this.addAccountData.id = this.dialogData.id
      }
      this.addAccountData.data = []
      this.addAccountData.show = true
    },
    addAccount (value,type,index) { //新建时-新增账户回调
      if (type == 'new') {
        this.parames.customerAccountList.push(JSON.parse(value))
      } else if (type == 'newEdit') { //新建时-修改人员信息
        this.parames.customerAccountList.splice(index,1,JSON.parse(value))
      }
    },
    handleEditAccount (index,row) { //修改
      this.addAccountData.title = '修改账户'
      this.addAccountData.index = index
      if (this.dialogData.type == 'new') {
        this.addAccountData.type = 'newEdit'
      } else if (this.dialogData.type == 'edit') {
        this.addAccountData.type = 'saveEdit'
      }
      this.addAccountData.data = JSON.stringify(row)
      this.addAccountData.show = true
    },
    handleCanleAccount (index,row) { //删除添加的账户
      if (this.dialogData.type == 'new') {
        this.parames.customerAccountList.splice(index,1)
      } else if (this.dialogData.type == 'edit') {
        this.$post(this.$api.postCustomerAccountDel,{
          customerAccountId: row.id
        }).then(response => {
          if (response.data.succeed) {
            this.render()
          }
        })
      }
    },
    handleClose() {//关闭弹窗
      this.dialogData.show = false;
    },
    handleSaveGongShang () { //保存工商信息
      this.gsData.customerId = this.dialogData.id
      if (this.gsData.industry) {
        this.gsData.industryId = this.gsData.industry[0]
        this.gsData.childIndustryId = this.gsData.industry[1]
      }
      this.$post(this.$api.postCustomerBusinessEdit,this.gsData).then(response => {
        if (response.data.succeed) {
        }
      })
    },
    handleSaveFinal () { //保存财务信息
      this.cwData.customerId = this.dialogData.id
      this.$post(this.$api.postCustomerFinanceEdit,this.cwData).then(response => {
        if (response.data.succeed) {
        }
      })
    },
    setData () {
      let userIdList = []
      this.parames.userIdList.forEach(el=>{
        userIdList.push(el.userId)
      })
      this.parames.userIdList = userIdList
    },
    handleSend () { //提交信息
      this.$refs['paramesb'].validate((valid) => {
        if (valid) {
          this.setData()
          this.loadingBtn = true
          if (this.dialogData.type == 'edit') { //修改保存成功--同时需要保存工商信息--财税信息
              this.$post(this.$api.postCustomerCoreEdit,this.parames).then(response => {
                if (response.data.succeed) {
                  this.handleSaveGongShang()
                  this.handleSaveFinal()
                  setTimeout(() => {
                    this.$message({ type:'success',message: '修改成功'})
                    this.handleClose()
                    this.$emit('checkOpen','1') //重新打开父级弹窗
                  }, 200);
                }
              })
            } else { //新增
              this.$post(this.$api.postCustomerCoreAdd,this.parames).then(response => {
                if (response.data.succeed) {
                  this.$message({ type:'success',message: '保存成功'})
                  this.$emit('checkRender')
                  this.handleClose()
                }
              })
            }
            setTimeout(() => {
              this.loadingBtn = false
            }, 400);
        }
      })
    },
    getCompanyType () { //获取企业类型
      this.$post(this.$api.postCustomerBaseInfoInCompanyTypeList,this.parames).then(response => {
          if (response.data.succeed) {
              this.companyData = response.data.data
          }
      })
    },
    getTree () { //获取树组织
      this.$post(this.$api.postCustomerBaseInfoIndustryTree).then(response => {
        if (response.data.succeed) {
          this.subjectData = []
          if (response.data.data.length > 0) {
            response.data.data.forEach(el=>{
              this.getData(el)
              if (el.label) {
                this.subjectData.push(el)
              }
            })
          }
        }
      })
    },
    getData(data) { //处理树数据
      if (data.industryName) {
        data.label = data.industryName
      }
      data.value = data.id
      if (data.childList.length>0) {
        data.children = data.childList
      }
      if (data.children && data.children.length > 0) {
        for(var i = 0; i < data.children.length; i++) {
          this.getData(data.children[i])
        }
      }
    },
    checkValue (value) { //获取来源信息
      this.parames.customerSourceType = value
    },
    checkRenderAccount () { //账户信息刷新
      this.$post(this.$api.postCustomerCoreFind,{
        customerId: this.dialogData.id
      }).then(response => {
        if (response.data.succeed) {
          let data = response.data.data
          this.parames.customerAccountList = data.customerAccountList
        }
      })
    },
    checkRenderUser () { //人员信息刷新
      this.$post(this.$api.postCustomerCoreFind,{
        customerId: this.dialogData.id
      }).then(response => {
        if (response.data.succeed) {
          let data = response.data.data
          this.parames.customerContactsList = data.customerContactsList
          this.parames.customerContactsList.forEach(el=>{
            let roleNames = []
            let contactsRole = []
            el.contactsRole.forEach(bl=>{
              roleNames.push(bl.desc)
              contactsRole.push(bl.name)
            })
            el.contactsRoleName = roleNames
            el.contactsRole = contactsRole
          })
        }
      })
    },
    checkRenderFile () { //刷新文件信息
      this.$post(this.$api.postCustomerCoreFind,{
        customerId: this.dialogData.id
      }).then(response => {
        if (response.data.succeed) {
          let data = response.data.data
          this.parames.customerFileList = data.customerFileList
        }
      })
    },
    render () { //获取详情信息
      this.loading = true
      this.$post(this.$api.postCustomerCoreFind,{
        customerId: this.dialogData.id
      }).then(response => {
        if (response.data.succeed) {
          this.activeNames = ['1','2','3','4', '5', '6']
          let data = response.data.data
          this.parames.id = data.id
          if (data.customerLabel) {
            this.parames.customerLabel = data.customerLabel //客户标签
          }
          this.parames.customerName = data.customerName //客户名称
          this.parames.shortName = data.shortName //简称
          this.parames.previousName = data.previousName //曾用名
          this.parames.creditCode = data.creditCode //社会信用代码
          if (data.customerLevel) {
            this.parames.customerLevel = data.customerLevel.name //客户等级
          }
          this.parames.businessPhone = data.businessPhone //企业电话
          this.parames.businessEmail = data.businessEmail //企业邮箱
          this.parames.address = data.address //联系地址
          this.parames.remark = data.remark //备注
          this.parames.userIdList = data.teamMemberList
          if (data.customerSourceType) {
            this.parames.customerSourceType = data.customerSourceType.name //客户来源类型
          }
          this.parames.customerSourceId = data.customerSourceId //客户来源
          // this.parames.director = data.director //负责人
          this.parames.customerContactsList = data.customerContactsList
          this.parames.customerContactsList.forEach(el=>{
            let roleNames = []
            let contactsRole = []
            el.contactsRole.forEach(bl=>{
              roleNames.push(bl.desc)
              contactsRole.push(bl.name)
            })
            el.contactsRoleName = roleNames
            el.contactsRole = contactsRole
          })
          this.parames.customerFileList = data.customerFileList //文件列表
          this.parames.customerAccountList = data.customerAccountList

          this.gsData.customerId = data.businessInfoDO.customerId//客户ID
          this.gsData.industry = [data.businessInfoDO.industryId,data.businessInfoDO.childIndustryId]
          this.gsData.companyTypeId = data.businessInfoDO.companyTypeId //企业类型ID
          this.gsData.artificialPerson = data.businessInfoDO.artificialPerson //法定代表人
          this.gsData.registeredCapital = data.businessInfoDO.registeredCapital //注册资金
          this.gsData.establishmentDate = data.businessInfoDO.establishmentDate //成立日期
          this.gsData.licenseDate = data.businessInfoDO.licenseDate //出执照日期
          this.gsData.businessDeadlineDate = data.businessInfoDO.businessDeadlineDate //营业期限截止
          this.gsData.approvalDate = data.businessInfoDO.approvalDate //核准日期
          this.gsData.administrativeRegion = data.businessInfoDO.administrativeRegion //注册行政区域ID
          this.gsData.registeredRegionId = data.businessInfoDO.registeredRegionId //注册区域ID
          this.gsData.registeredAddress = data.businessInfoDO.registeredAddress //注册详细地址
          this.gsData.businessScope = data.businessInfoDO.businessScope //经营范围ID
          this.gsData.businessStartDate = data.businessInfoDO.businessStartDate
          this.gsData.businessEndDate = data.businessInfoDO.businessEndDate

          if (data.financialInfoDO) {
            if (data.financialInfoDO.customerId) {
              this.cwData.customerId = data.financialInfoDO.customerId //客户ID
            }
            if (data.financialInfoDO.taxType) {
              this.cwData.taxType = data.financialInfoDO.taxType.name //财税类型（枚举）
            }
            if (data.financialInfoDO.declareTaxType) {
              this.cwData.declareTaxType = data.financialInfoDO.declareTaxType.name //报税类别（枚举）
            }
            if (data.financialInfoDO.taxPlate) {
              this.cwData.taxPlate = data.financialInfoDO.taxPlate.name //税盘（枚举）
            }
            this.cwData.zeroDeclaration = data.financialInfoDO.zeroDeclaration //是否零申报（布尔）
            this.cwData.taxOffice = data.financialInfoDO.taxOffice //税务所
            this.cwData.franchiseeName = data.financialInfoDO.franchiseeName //专营员姓名
            this.cwData.franchiseePhone = data.financialInfoDO.franchiseePhone //专营员电话
          }
          setTimeout(() => {
            this.loading = false
          }, 200);
        }
      })
    },
    resetData () { //重置数据
      this.activeNames = ['1','2','3','4']
      this.parames.id = ''
      this.parames.customerLabel = [] //客户标签
      this.parames.customerName = "" //客户名称
      this.parames.shortName = "" //简称
      this.parames.previousName = "" //曾用名
      this.parames.creditCode = '' //社会信用代码
      this.parames.customerLevel = 'POTENTIAL_CUSTOMER' //客户等级
      this.parames.businessPhone = '' //企业电话
      this.parames.businessEmail = '' //企业邮箱
      this.parames.address = '' //联系地址
      this.parames.remark = '' //备注
      this.parames.customerSourceType = '' //客户来源类型
      this.parames.customerSource = '' //客户来源
      // this.parames.director = '' //负责人
      this.parames.customerContactsList = []
      this.parames.customerFileList = [] //文件列表
      this.parames.customerAccountList = []
      this.gsData.customerId = ''//客户ID
      this.gsData.industryId = '' //主营行业ID
      this.gsData.childIndustryId = '' //细分行业ID
      this.gsData.industry = []
      this.gsData.companyTypeId = '' //企业类型ID
      this.gsData.artificialPerson = '' //法定代表人
      this.gsData.registeredCapital = '' //注册资金
      this.gsData.establishmentDate = '' //成立日期
      this.gsData.licenseDate = '' //出执照日期
      this.gsData.businessDeadlineDate = '' //营业期限截止
      this.gsData.approvalDate = '' //核准日期
      this.gsData.administrativeRegionId = [] //注册行政区域ID
      this.gsData.registeredRegionId = [] //注册区域ID
      this.gsData.registeredAddress = '' //注册详细地址
      this.gsData.businessScope = '' //经营范围ID
      this.gsData.businessStartDate = ''
      this.gsData.businessEndDate = ''

      this.cwData.customerId = '' //客户ID
      this.cwData.taxType = '' //财税类型（枚举）
      this.cwData.declareTaxType = '' //报税类别（枚举）
      this.cwData.zeroDeclaration = '' //是否零申报（布尔）
      this.cwData.taxOffice = '' //税务所
      this.cwData.taxPlate = '' //税盘（枚举）
      this.cwData.franchiseeName = '' //专营员姓名
      this.cwData.franchiseePhone = '' //专营员电话
      this.parames.userIdList = []
      this.parames.customerContactsList = []
      this.$refs.paramesb.resetFields()
    },
  },
  components: {
    addUserhDialog,
    addFileDialog,
    addAccountDialog
  },
  computed: {
      changeDialog () {
        return this.dialogData.show
      }
    }, 
    watch: {
      changeDialog: {
        handler (curVal, oldVal) {
          if (curVal) {
            let userInfo = this.$utils.getLocalStorage('user')
            if (this.dialogData.type == 'new') {
              this.parames.userIdList.push({
                userName: userInfo.name,
                userId: userInfo.id
              })
            }
            this.$utils.getEnumberation('postCustomerCoreTypeFind','customerLabelData',this) //客户标签
            this.$utils.getEnumberation('postCustomerLevel','customerLevel',this) //客户等级
            this.$utils.getEnumberation('postCustomerTaxType','taxType',this) //财税类型
            this.$utils.getEnumberation('postCustomerDeclareTaxType','declarTaxType',this) //报税类别
            this.$utils.getEnumberation('postCustomerTaxPlate','taxPlate',this) //税盘枚举
            if (this.dialogData.type == 'edit') {
              this.getCompanyType()
              this.getTree()
              this.render()
            }
          } else {
            this.resetData()
          }
        }
      }
    },
};
</script>

<style scoped>
</style>
